package com.zline.foodraise.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.photoselector.listener.OnPhotoItemCheckedListener;
import com.photoselector.listener.PhotoItemListener;
import com.zline.foodraise.listener.RecyclerItemClicked;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.UiThread;

/**
 * Created by heshantao on 15/6/27.
 * RecyclerView 适配器基础类
 */
@EBean
public abstract class RecyclerViewBaseAdapter<T, V extends View> extends RecyclerView.Adapter<RecyclerViewBaseAdapter.ViewWrapper<V>> {
    private static final String LOG_TAG = RecyclerViewBaseAdapter.class.getSimpleName();
    public SparseArray<T> arrys;
    RecyclerItemClicked itemClicked;
    OnPhotoItemCheckedListener checkedListener;
    PhotoItemListener photoItemListener;
    int parentPosition=-1;
    int flag;

    public void setAdapterData(SparseArray<T> sparseArray) {
        this.arrys = sparseArray;
    }

    public void setRecycleItemClickListener(RecyclerItemClicked itemClickListener){
        this.itemClicked=itemClickListener;
    }

    public void setPhotoItemCheckListener(OnPhotoItemCheckedListener checkListener){
        this.checkedListener=checkListener;
    }

    public void setPhotoItemListener(PhotoItemListener listener){
        this.photoItemListener=listener;
    }

    public SparseArray<T> getArrys(){
        return arrys;
    }

    public void setParentPostion(int postion){
        this.parentPosition=postion;
    }
    public void setRecycleFlag(int flag) {
        this.flag = flag;
    }

    @Override
    public int getItemCount() {
        return arrys.size();
    }

    @Override
    public ViewWrapper<V> onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewWrapper<V>(onCreateItemView(parent, viewType));
    }


    protected abstract V onCreateItemView(ViewGroup parent, int viewType);

    static class ViewWrapper<V extends View> extends RecyclerView.ViewHolder {

        private V view;

        public ViewWrapper(V itemView) {
            super(itemView);
            view = itemView;
        }

        public V getView() {

            return view;
        }
    }


    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void addItem(int position,T t) {
        arrys.put(position, t);
        notifyItemInserted(position);

    }


    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void removeItem(int position) {
        if(position>arrys.size()){
            return;
        }
            arrys.remove(position);
            notifyItemRemoved(position);
    }



    //主要用于从任意位置删除并且添加
    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void removeFromMiddleItem(int position) {
        if(position>arrys.size()){
            return;
        }else if(position==arrys.size()-1){
            arrys.remove(position);
            notifyItemRemoved(position);
        }else{
            SparseArray<T> tempArray=arrys;
            for(int i=position;i<tempArray.size()-1;i++){
                arrys.put(i,tempArray.get(i+1));
            }
            arrys.remove(getItemCount()-1);
            notifyDataSetChanged();

        }
    }

    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void addAll(SparseArray<T> items) {
        int startIndex = this.arrys.size();
        for(int i=0;i<items.size();i++){
            int key=items.keyAt(i);
            this.arrys.put(startIndex + i, items.get(key));
        }
        notifyItemRangeInserted(startIndex, items.size());
    }

    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void clear() {
        int size = arrys.size();
        arrys.clear();
        notifyItemRangeRemoved(0, size);
    }
}
